package com.lry.basic.juc;

public class WriteCombine {

    private static final int IT = Integer.MAX_VALUE;
    private static final  int ITEMS = 1<<24;
    private static final int MASK = ITEMS-1;

    private static final byte[] arrA = new byte[ITEMS];
    private static final byte[] arrB = new byte[ITEMS];
    private static final byte[] arrC = new byte[ITEMS];
    private static final byte[] arrD = new byte[ITEMS];
    private static final byte[] arrE = new byte[ITEMS];
    private static final byte[] arrF = new byte[ITEMS];

    public static void main(String[] args) {
        for(int i=0;i<3;i++){
            System.out.println("runCaseOne:"+runCaseOne());
            System.out.println("runCaseTwo:"+runCaseTwo());
        }
    }

    public static long runCaseOne(){
        long start = System.currentTimeMillis();

        int i = IT;
        while(--i!=0){
            int slot = i&MASK;
            byte b = (byte)i;

            arrA[slot] = b;
            arrB[slot] = b;
            arrC[slot] = b;
            arrD[slot] = b;
            arrE[slot] = b;
            arrF[slot] = b;
        }
        return System.currentTimeMillis()-start;
    }

    public static long runCaseTwo(){
        long start = System.currentTimeMillis();
        int i = IT;
        while(--i!=0){
            int slot = i&MASK;
            byte b = (byte)i;

            arrA[slot] = b;
            arrB[slot] = b;
            arrC[slot] = b;
        }
        i = IT;
        while(--i!=0){
            int slot = i&MASK;
            byte b = (byte)i;

            arrD[slot] = b;
            arrE[slot] = b;
            arrF[slot] = b;
        }
        return System.currentTimeMillis()-start;
    }
}
